package org.example.admin.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.admin.exception.BizException;
import org.example.admin.mapper.NotifyMapper;
import org.example.admin.service.NotifyService;
import org.example.entity.Notify;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class NotifyServiceImpl implements NotifyService {

    private NotifyMapper notifyMapper;

    NotifyServiceImpl(NotifyMapper notifyMapper){
        this.notifyMapper = notifyMapper;
    }

    @Override
    public Notify getMsgById(Integer id) throws BizException {
        Notify notify = null;
        try {
            notify = notifyMapper.selectById(id);

//            Integer.parseInt("ab");
        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层查询异常: " + ex.getMessage());
        }
        return notify;
    }

    @Override
    public List<Notify> getUnreadMsgListByUId(Integer uId) throws BizException{

        List<Notify> notifyList = null;

        try {
            notifyList = notifyMapper.selectUnreadMsgListByUId(uId);

//            Integer.parseInt("ab");
        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层查询异常: " + ex.getMessage());
        }
        return notifyList;
    }

    @Override
    public void insertNotify(Notify notify)throws BizException {

        try {
            notifyMapper.insert(notify);
//            Integer.parseInt("ab");
        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层插入异常: " + ex.getMessage());
        }
    }

    @Override
    public void deleteNotifyById(Integer id)throws BizException {
        try {
            //真删除。需要留意关联表的设置
            notifyMapper.deleteById(id);
            //假删除
            //notifyMapper.update();
        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层插入异常: " + ex.getMessage());
        }
    }

    //批量删除

    @Override
    public void updateNotify(Notify notify)throws BizException {


        try {
            //根据实体类的id属性自己更新。
            notifyMapper.updateById(notify);

        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层插入异常: " + ex.getMessage());
        }
    }

    @Override
    public List<Notify> getAllMsg() throws BizException{
        List<Notify> notifyList = null;

        try {
            //获取所有消息
            notifyList = notifyMapper.selectList(Wrappers.emptyWrapper());

//            Integer.parseInt("ab");
        }catch (Exception ex) {
            ex.printStackTrace();
            throw new BizException("业务层查询异常: " + ex.getMessage());
        }
        return notifyList;
    }
}
